﻿using Common.Tool.FileTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Media;

namespace Common.Tool.ControlTools
{
    public class RichTextBoxHelper
    {
        /// <summary>
        /// 清空
        /// </summary>
        public static void ClearText(RichTextBox RichTextBoxInfo)
        {
            try
            {
                lock (RichTextBoxInfo)
                {
                    RichTextBoxInfo.Dispatcher.Invoke((Action)delegate
                    {
                        //清空文本
                        RichTextBoxInfo.Document.Blocks.Clear();
                        //滚动到开头
                        RichTextBoxInfo.ScrollToVerticalOffset(0);
                    });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 插入文本
        /// </summary>
        static void AppendText(RichTextBox RichTextBoxInfo, System.Windows.Media.Brush brush, string temp, string msg, DateTime? time = null)
        {
            try
            {
                if (RichTextBoxInfo == null)
                    return;
                var p = new Paragraph(); // Paragraph 类似于 html 的 P 标签
                var r = new Run((time?? DateTime.Now).ToString("HH:mm:ss.fff") + $" [{temp}] " + msg); // Run 是一个 Inline 的标签
                p.Inlines.Add(r);
                p.Foreground = brush;//设置字体颜色
                p.Margin = new System.Windows.Thickness(3, 6, 3, 3);
                lock (RichTextBoxInfo)
                {
                    RichTextBoxInfo.Dispatcher.Invoke((Action)delegate
                    {
                        //添加文本
                        RichTextBoxInfo.Document.Blocks.Add(p);
                        //todo 验证
                        if (RichTextBoxInfo.Document.Blocks.Count > 5000)
                        {
                            RichTextBoxInfo.Document.Blocks.Remove(RichTextBoxInfo.Document.Blocks.FirstBlock);
                        }
                        //滚动到末尾
                        RichTextBoxInfo.ScrollToVerticalOffset(RichTextBoxInfo.Document.Blocks.Count * 200);
                    });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 插入一级Title
        /// </summary>
        public static void AppendTile1(RichTextBox RichTextBoxInfo, string temp, string msg,DateTime? time=null)
        {
            AppendText(RichTextBoxInfo, Brushes.RoyalBlue, temp, msg,time);
        }
        /// <summary>
        /// 插入二级Title
        /// </summary>
        public static void AppendTile2(RichTextBox RichTextBoxInfo, string temp, string msg, DateTime? time = null)
        {
            AppendText(RichTextBoxInfo, Brushes.DodgerBlue, temp, msg, time);
        }
        /// <summary>
        /// 插入信息
        /// </summary>
        public static void AppendInfo(RichTextBox RichTextBoxInfo, string temp, string msg, DateTime? time = null)
        {
            AppendText(RichTextBoxInfo, Brushes.Black, temp, msg, time);
        }
        /// <summary>
        /// 插入通信
        /// </summary>
        public static void AppendCommunication(RichTextBox RichTextBoxInfo, string temp, string msg, DateTime? time = null)
        {
            AppendText(RichTextBoxInfo, Brushes.DimGray, temp, msg, time);
        }
        /// <summary>
        /// 插入提示
        /// </summary>
        public static void AppendTip(RichTextBox RichTextBoxInfo, string temp, string msg, DateTime? time = null)
        {
            AppendText(RichTextBoxInfo, Brushes.OrangeRed, temp, msg, time);
        }
        /// <summary>
        /// 插入结果
        /// </summary>
        public static void AppendResult(RichTextBox RichTextBoxInfo, string temp, string msg, DateTime? time = null)
        {
            AppendText(RichTextBoxInfo, Brushes.DeepSkyBlue, temp, msg, time);
        }
        /// <summary>
        /// 插入错误
        /// </summary>
        public static void AppendErr(RichTextBox RichTextBoxInfo, string temp, string msg, DateTime? time = null)
        {
            AppendText(RichTextBoxInfo, Brushes.Tomato, temp, msg, time);
        }
    }
}
